{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "3fede680",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7ee82745",
   "metadata": {},
   "source": [
    "## 使用numpy读写数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "de3af800",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting tang.txt\n"
     ]
    }
   ],
   "source": [
    "%%writefile tang.txt\n",
    "1 2 3 4 5 6\n",
    "4 5 6 7 8 9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "49a747bb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 2., 3., 4., 5., 6.],\n",
       "       [4., 5., 6., 7., 8., 9.]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data =[]\n",
    "with open('tang.txt') as f:\n",
    "    for line in f.readlines():\n",
    "        fileds = line.split()\n",
    "        cur_data = [float(x) for x in fileds]\n",
    "        data.append(cur_data)\n",
    "        \n",
    "data = np.array(data)\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "a3e4dc1d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 2., 3., 4., 5., 6.],\n",
       "       [4., 5., 6., 7., 8., 9.]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.loadtxt('tang.txt')\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "262b3586",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting tang.txt\n"
     ]
    }
   ],
   "source": [
    "%%writefile tang.txt\n",
    "1,2,3,4,5,6\n",
    "4,5,6,7,8,9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "12dd8656",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 2., 3., 4., 5., 6.],\n",
       "       [4., 5., 6., 7., 8., 9.]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.loadtxt('tang.txt',delimiter=',')\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "78253db0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting tang2.txt\n"
     ]
    }
   ],
   "source": [
    "%%writefile tang2.txt\n",
    "x,y,z,w,a,b\n",
    "1,2,3,4,5,6\n",
    "4,5,6,7,8,9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "2b8ff0fe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 2., 3., 4., 5., 6.],\n",
       "       [4., 5., 6., 7., 8., 9.]])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.loadtxt('tang2.txt',delimiter=',',skiprows=1)\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "00e92b90",
   "metadata": {},
   "source": [
    "* ‘tang2.txt’：路径最好放在和代码一起\n",
    "* skiprows: 去掉几行\n",
    "* delimiter: 分隔符\n",
    "* usecols=(0,1,4): 指定使用哪几列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "2378f623",
   "metadata": {},
   "outputs": [],
   "source": [
    "tang_array = np.array([[1,2,3],[4,5,6]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "4fc243b5",
   "metadata": {},
   "outputs": [],
   "source": [
    "np.savetxt('tang4.txt',tang_array)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "787b245f",
   "metadata": {},
   "outputs": [],
   "source": [
    "np.savetxt('tang4.txt',tang_array,fmt='%d')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "ba701b7c",
   "metadata": {},
   "outputs": [],
   "source": [
    "np.savetxt('tang4.txt',tang_array,fmt='%d',delimiter=',')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "4089e2e8",
   "metadata": {},
   "outputs": [],
   "source": [
    "np.savetxt('tang4.txt',tang_array,fmt='%.2f',delimiter=',')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "de862100",
   "metadata": {},
   "source": [
    "## 读写array结构"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "baa45828",
   "metadata": {},
   "outputs": [],
   "source": [
    "tang_array = np.array([[1,2,3],[4,5,6]])\n",
    "np.save('tang_array.npy',tang_array)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "a7166b58",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang =np.load('tang_array.npy')\n",
    "tang"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "fc16d011",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang_array2 = np.arange(10)\n",
    "tang_array2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "cee316fc",
   "metadata": {},
   "outputs": [],
   "source": [
    "np.savez('tang.npz',a=tang_array,b=tang_array2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "be16dce8",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = np.load('tang.npz')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "e1b56e22",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Keys in the NPZ file: ['a', 'b']\n",
      "Array 'a': [[1 2 3]\n",
      " [4 5 6]]\n",
      "Array 'b': [0 1 2 3 4 5 6 7 8 9]\n"
     ]
    }
   ],
   "source": [
    "# 查看所有键名\n",
    "print(\"Keys in the NPZ file:\", list(data.keys()))\n",
    "\n",
    "# 访问并输出各个数组\n",
    "print(\"Array 'a':\", data['a'])\n",
    "print(\"Array 'b':\", data['b'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f9c0dfd1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
